package git.soulbgm.pojo.dto;

import git.soulbgm.utils.DateUtil;

import java.util.Calendar;

/**
 * 文件信息dto
 *
 * @author SoulBGM
 * @date 2024/02/07
 */
public class FileInfoDto {

    /**
     * 文件名
     */
    private String fileName;
    /**
     * 是否是目录
     */
    private boolean isDirectory = false;
    /**
     * 子目录数量
     */
    private int subDirectoryNum;
    /**
     * 所有者
     */
    private String owner;
    /**
     * 所属组
     */
    private String groups;
    /**
     * 文件大小(字节)
     */
    private long fileSize;
    /**
     * 最后修改时间
     */
    private String lastModified;

    /**
     * 时间连接符号数量 -的数量
     */
    private static final int TIME_JOIN_SYMBOL_COUNT = 6;

    public FileInfoDto() {
    }

    public FileInfoDto(String output) {
        String[] columns = output.trim().split("[ \t]+");
        if (columns.length < 7) {
            return;
        }
        isDirectory = columns[0].startsWith("d");
        subDirectoryNum = isDirectory ? Integer.parseInt(columns[1]) - 2 : 0;
        owner = columns[2];
        groups = columns[3];
        fileSize = Long.parseLong(columns[4]);
        int timeJoinSymbolCount = columns[5].length() - columns[5].replace("-", "").length();
        int startIndex = 8;
        if (timeJoinSymbolCount == TIME_JOIN_SYMBOL_COUNT) {
            startIndex = 6;
            lastModified = DateUtil.format(DateUtil.parse(columns[5], DateUtil.NORM_DATETIME_MS_FORMATTER), DateUtil.NORM_DATETIME_MS_PATTERN);
        } else {
            Calendar calendar = getCalendar(columns[5], columns[6], columns[7]);
            lastModified = DateUtil.format(calendar.getTime(), DateUtil.FORMAT_YMD_HM);
        }
        StringBuilder fileName = new StringBuilder();
        for (int i = startIndex; i < columns.length; i++) {
            fileName.append(columns[i]);
            if (i != columns.length - 1) {
                fileName.append(" ");
            }
        }
        this.fileName = fileName.toString();
    }

    private Calendar getCalendar(String month, String day, String hourOrYear) {
        Calendar calendar = DateUtil.getStartCalendar(DateUtil.getCalendar(), DateUtil.MINUTE);
        setMonth(calendar, month);
        calendar.set(DateUtil.DAY, Integer.parseInt(day));
        String[] split = hourOrYear.split(":");
        if (split.length == 1) {
            calendar.set(DateUtil.YEAR, Integer.parseInt(split[0]));
            calendar.set(DateUtil.HOUR, calendar.getActualMinimum(DateUtil.HOUR));
            calendar.set(DateUtil.MINUTE, calendar.getActualMinimum(DateUtil.MINUTE));
        } else if (split.length == 2) {
            calendar.set(DateUtil.HOUR, Integer.parseInt(split[0]));
            calendar.set(DateUtil.MINUTE, Integer.parseInt(split[1]));
        }
        return calendar;
    }

    private void setMonth(Calendar calendar, String month) {
        if ("Jan".equalsIgnoreCase(month) || "1月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.JANUARY);
        } else if ("Feb".equalsIgnoreCase(month) || "2月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.FEBRUARY);
        } else if ("Mar".equalsIgnoreCase(month) || "3月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.MARCH);
        } else if ("Apr".equalsIgnoreCase(month) || "4月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.APRIL);
        } else if ("May".equalsIgnoreCase(month) || "5月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.MAY);
        } else if ("Jun".equalsIgnoreCase(month) || "6月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.JUNE);
        } else if ("Jul".equalsIgnoreCase(month) || "7月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.JULY);
        } else if ("Aug".equalsIgnoreCase(month) || "8月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.AUGUST);
        } else if ("Sept".equalsIgnoreCase(month) || "9月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.SEPTEMBER);
        } else if ("Oct".equalsIgnoreCase(month) || "10月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.OCTOBER);
        } else if ("Nov".equalsIgnoreCase(month) || "11月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.NOVEMBER);
        } else if ("Dec".equalsIgnoreCase(month) || "12月".equalsIgnoreCase(month)) {
            calendar.set(DateUtil.MONTH, Calendar.DECEMBER);
        }
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public boolean isDirectory() {
        return isDirectory;
    }

    public void setDirectory(boolean directory) {
        isDirectory = directory;
    }

    public int getSubDirectoryNum() {
        return subDirectoryNum;
    }

    public void setSubDirectoryNum(int subDirectoryNum) {
        this.subDirectoryNum = subDirectoryNum;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getGroups() {
        return groups;
    }

    public void setGroups(String groups) {
        this.groups = groups;
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }

    public String getLastModified() {
        return lastModified;
    }

    public void setLastModified(String lastModified) {
        this.lastModified = lastModified;
    }

    @Override
    public String toString() {
        return "FileInfoDto{" +
                "fileName='" + fileName + '\'' +
                ", isDirectory=" + isDirectory +
                ", subDirectoryNum=" + subDirectoryNum +
                ", owner='" + owner + '\'' +
                ", groups='" + groups + '\'' +
                ", fileSize=" + fileSize +
                ", lastModified='" + lastModified + '\'' +
                '}';
    }
}
